#!/usr/bin/env python
#coding=utf-8
import itertools
import string
import hashlib

#md5(), sha1(), sha224(), sha256(), sha384(), and sha512()

#可看不可用的函数
def dic(max, min=1):
    chars=string.printable[:-5]
    p=[]
    for i in xrange(min,max+1):
        p.append((itertools.product(chars,repeat=i),))
    d = itertools.chain(*p)
    cypherdic = []
    for x in d:
        for y in x:
            y="".join(y)
            cypherdic.append(y)
    return cypherdic

def crackMD5(ciphertext, max, min=1):
    chars=string.printable[:-5]
    p=[]
    for i in xrange(min,max+1):
        p.append((itertools.product(chars,repeat=i),))
    d = itertools.chain(*p)
    for x in d:
        for y in x:
            y="".join(y)
            passwd = hashlib.md5(y).hexdigest()    #########################这里替换算法即可
            if passwd == ciphertext:
            	return "MD5 Crack success! The password is : " + y
    return "MD5 Crack failed!"


def crackSHA1(ciphertext, max, min=1):
    chars=string.printable[:-5]
    p=[]
    for i in xrange(min,max+1):
        p.append((itertools.product(chars,repeat=i),))
    d = itertools.chain(*p)
    for x in d:
        for y in x:
            y="".join(y)
            passwd = hashlib.sha1(y).hexdigest()    #########################这里替换算法即可
            if passwd == ciphertext:
            	return "SHA1 Crack success! The password is : " + y
    return "SHA1 Crack failed!"


def main():
    print crackMD5('4124bc0a9335c27f086f24ba207a4912',2);
    print crackSHA1('e0c9035898dd52fc65c41454cec9c4d2611bfb37',2);


if __name__ == '__main__':
    main()